using System;
using Habanero.Base;
using Habanero.Base.Exceptions;

namespace FireStarterModeller.BO
{
    public class DMInheritanceRelationshipDiscriminatorMustBeCompulsoryAndWriteOnceOrWriteNew : IBusinessObjectRule
    {
        private DMInheritanceRelationship _inheritanceRelationship;

//        public DMInheritanceRelationshipDiscriminatorMustBeCompulsoryAndWriteOnceOrWriteNew(DMInheritanceRelationship inheritanceRelationship)
//        {
//            if (inheritanceRelationship == null) throw new ArgumentNullException("inheritanceRelationship");
//            _inheritanceRelationship = inheritanceRelationship;
//        }

        public bool IsValid()
        {
            throw new HabaneroApplicationException("The IsValid is no longer Valid you should use the IsValid(bo) method");
        }

        public bool IsValid(IBusinessObject bo)
        {
            _inheritanceRelationship = (DMInheritanceRelationship) bo;
            if (_inheritanceRelationship.InheritanceType == ORMapping.ConcreteTableInheritance) return true;
            if (_inheritanceRelationship.Discriminator == null) return false;
            IDMProperty dmProperty = _inheritanceRelationship.GetDiscriminatorProp();
            if (dmProperty == null) return false;
            return (IsWriteOnceOrWriteNew((DMProperty)dmProperty)) && (dmProperty.IsCompulsory);
        }

        private bool IsWriteOnceOrWriteNew(DMProperty dmProperty)
        {
            return dmProperty.ReadWriteRule == PropReadWriteRule.WriteOnce || dmProperty.ReadWriteRule == PropReadWriteRule.WriteNew;
        }

        public string Name
        {
            get { return "DMInheritanceRelationshipDiscriminatorMustBeCompulsoryAndWriteOnceOrWriteNew"; }
        }

        public string Message
        {
            get { return string.Format("The Discriminator Property for the Inheritance Relationship '" + _inheritanceRelationship +
                "' is a compulsory field and the ReadWriteRule must be WriteOnce or WriteNew", _inheritanceRelationship.SubClass); }
        }

        public ErrorLevel ErrorLevel
        {
            get { return ErrorLevel.Warning; }
        }
    }
}